#!/usr/bin/env python
#coding=utf-8

import unittest
import parse

class parseTest(unittest.TestCase): 
   
    def testParseMain(self):  
        f = open(r'file/main.txt', 'r')
        data = parse.parseMain(f)  
        self.assertEqual(data['jindou'], '5535')  
           
        f = open(r'file/login_error.txt', 'r')
        data = parse.parseMain(f)  
        self.assertEqual(data['error'], '登录账号或密码错误，请重试') 
         
        f = open(r'file/main_error.txt', 'r')
        data = parse.parseMain(f)  
        self.assertFalse(data) 
        
    def testParseFriendsList(self):  
        f = open(r'file/friends_list.txt', 'r')
        data = parse.parseFriendsList(f)  
        self.assertEqual(data[0], {'name': '潘华聪', 'id': '12185053'}) 
        self.assertEqual(len(data), 25)
        
        f = open(r'file/main_error.txt', 'r')
        data = parse.parseFriendsList(f)  
        self.assertFalse(data) 
    
    #############blog#################            
    def testParseMyBlogList(self):  
        f = open(r'file/my_blog_list.txt', 'r')
        data = parse.parseMyBlogList(f)  
        self.assertEqual(data, {'id': '920561'}) 
        
        f = open(r'file/main_error.txt', 'r')
        data = parse.parseMyBlogList(f)  
        self.assertFalse(data) 
                
    def testParsePostBlog(self):  
        f = open(r'file/post_blog.txt', 'r')
        data = parse.parsePostBlog(f)  
        self.assertEqual(data, 13844833) 
        
        f = open(r'file/main_error.txt', 'r')
        data = parse.parsePostBlog(f)  
        self.assertFalse(data) 
        
    def testParseBlogList(self):  
        f = open(r'file/blog_list.txt', 'r')
        data = parse.parseBlogList(f)  
        self.assertEqual(data, {'time':'7小时前', 'title':'男女相差几岁结婚最合适(转)', 'blogid':'13844329', 'id':'502604'}) 
        
        f = open(r'file/main_error.txt', 'r')
        data = parse.parseBlogList(f)  
        self.assertFalse(data) 
        
    def testParseSaveBlogError(self):  
        f = open(r'file/save_blog_error.txt', 'r')
        data = parse.parseSaveBlogError(f)  
        self.assertEqual(data['state'], 0)
        self.assertEqual(data['info'], u'\u8bc4\u8bba\u6216\u56de\u590d\u7684\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a\u4e14\u957f\u5ea6\u4e0d\u80fd\u8d85\u8fc72000\u4e2a\u5b57\u7b26')  
        f = open(r'file/save_blog_jiafen.txt', 'r')
        data = parse.parseSaveBlogError(f)  
        self.assertEqual(data['state'], 1)
        f = open(r'file/save_blog_no_jiafen.txt', 'r')
        data = parse.parseSaveBlogError(f)  
        self.assertEqual(data['state'], 2)
        
        f = open(r'file/main_error.txt', 'r')
        data = parse.parseSaveBlogError(f)  
        self.assertFalse(data) 
        
    #############miniblog#################      
    def testParsePostTwitter(self):  
        f = open(r'file/post_twitter.txt', 'r')
        data = parse.parsePostTwitter(f)  
        self.assertEqual(data, 23831195) 
        
        f = open(r'file/main_error.txt', 'r')
        data = parse.parsePostTwitter(f)  
        self.assertFalse(data) 
        
    def testParseMiniBlogList(self):  
        f = open(r'file/miniblog_list.txt', 'r')
        data = parse.parseMiniBlogList(f)  
        self.assertEqual(data, {'userId': '491681', 'id': '23848241'}) 
        
        f = open(r'file/main_error.txt', 'r')
        data = parse.parseBlogList(f)  
        self.assertFalse(data)         
    
    ###########################            
    def testParseDaka(self): 
        f = open(r'file/daka_error.txt', 'r')
        data = parse.parseDaka(f)  
        self.assertEqual(data['state'], 0)  
        
        f = open(r'file/daka.txt', 'r')
        data = parse.parseDaka(f)  
        self.assertEqual(data['state'], 1) 
        self.assertEqual(data['info'], 50) 
        
        f = open(r'file/daka_haslingguo.txt', 'r')
        data = parse.parseDaka(f)  
        self.assertEqual(data['state'], 2) 
        
        f = open(r'file/main_error.txt', 'r')
        data = parse.parseDaka(f) 
        self.assertFalse(data)  
    
        
if __name__ == "__main__":
    unittest.main()   
